package com.sxbang.bms.controller;

import com.sxbang.bms.base.result.Results;
import com.sxbang.bms.dto.UserDto;
import com.sxbang.bms.model.SysUser;
import com.sxbang.bms.page.PageTableHandler;
import com.sxbang.bms.page.PageTableRequest;
import com.sxbang.bms.page.PageTableResponse;
import com.sxbang.bms.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.WebRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("user")
@Slf4j
public class UserController {

	@Autowired
	private UserService userService;

	@GetMapping("/{id}")
	public SysUser user(@PathVariable Long id) {
		return userService.getUserById(id);
	}

	@RequestMapping("/list")
	@ResponseBody
	public Results<SysUser> getUsers() {
		Results<SysUser> results = new Results<SysUser>();
		results.setCode("0");
		results.setCount(50);
		results.setData(userService.getAllUsers());
		results.setMsg("test");
		log.info("getUsers()--------------");
        log.debug("getUsers()--------------");
		return results;
	}

    @GetMapping
    @RequestMapping("/listPage")
    public PageTableResponse listUsers(PageTableRequest request) {
        return new PageTableHandler(new PageTableHandler.CountHandler() {

            @Override
            public int count(PageTableRequest request) {
                //return userService.count(request.getParams());
                return 1;
            }
        }, new PageTableHandler.ListHandler() {

            @Override
            public List<SysUser> list(PageTableRequest request) {
//                List<SysUser> list = userService.list(request.getParams(), request.getOffset(), request.getLimit());
//                return list;
                return null;
            }
        }).handle(request);
    }

	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public String editUser(Model model, SysUser user) {
		model.addAttribute("sysUser",userService.getUserById(user.getId()));
		return "user/user-edit";
	}

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public Results<SysUser> updateUser(UserDto userDto) {
        userService.updateUser(userDto);
        return Results.success();
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String addUser(Model model) {
        model.addAttribute("sysUser",new SysUser());
        return "user/user-add";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Results<SysUser> saveUser(UserDto userDto) {
        userDto.setStatus(1);
        userService.save(userDto);
        return Results.success();
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    @ResponseBody
    public Results<SysUser> deleteUser(UserDto userDto) {
        userService.deleteUser(userDto.getId());
        return Results.success();
    }

    String pattern = "yyyy-MM-dd";

    //只需要加上下面这段即可，注意不能忘记注解
    @InitBinder
    public void initBinder(WebDataBinder binder, WebRequest request) {
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat(pattern), true));// CustomDateEditor为自定义日期编辑器
    }
}
